package com.movitech.mobile.cz.base.supplier.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.ListObjCopyUtil;
import com.movitech.mobile.cz.base.common.util.excel.ExportExcel;
import com.movitech.mobile.cz.base.modules.config.SupplierStatusEnum;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.supplier.bizvo.*;
import com.movitech.mobile.cz.base.supplier.entity.SSuppliers;
import com.movitech.mobile.cz.base.supplier.service.SSupplierFormModifyMappingService;
import com.movitech.mobile.cz.base.supplier.service.SSuppliersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * SSuppliers 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sSuppliers")
@Api(value = "供方基本情况基础操作接口", description = "供方基本情况基础操作接口")
public class SSuppliersController {
    @Autowired
    @Qualifier("sSuppliersService")
    private SSuppliersService sSuppliersService;

    @Autowired
    private SSupplierFormModifyMappingService sSupplierFormModifyMappingService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有供方基本情况接口",
            notes = "获取所有供方基本情况接口",
            response = SSuppliers.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SSuppliers> page = sSuppliersService.selectPage(new Page<SSuppliers>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "供方基本情况列表查询接口",
            notes = "供方基本情况列表查询接口",
            response = SSuppliers.class
    )
    public RestReturnResult searchList(@RequestBody SSuppliers sSuppliers,
                                       @ApiParam(value = "分页页数") @PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SSuppliers> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sSuppliers);
        page = sSuppliersService.selectPage(page, wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/selectIdAndNameByEntity")
    @ApiOperation(
            value = "查询供方",
            notes = "查询供方",
            response = SSuppliers.class
    )
    public RestReturnResult selectSuppliers(@RequestBody SSuppliers sSuppliers
                                       ) throws Exception {
        RestReturnResult restReturnResult = RestReturnResult.success();
        EntityWrapper wrapper = new EntityWrapper<>();
        sSuppliers.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        wrapper.setEntity(sSuppliers);
        wrapper.setSqlSelect("id","company_name");
        restReturnResult.setData(ListObjCopyUtil.copyTo(sSuppliersService.selectList(wrapper), SupplieIDandnameVo.class));
        return restReturnResult;
    }

    @GetMapping("/getSSuppliers/{id}")
    @ApiOperation(
            value = "供方基本情况对象ID获取接口",
            notes = "供方基本情况对对象ID获取接口",
            response = SSuppliers.class
    )
    public RestReturnResult<SuppliersDetailVO> getSuppliersById(@PathVariable String id) {
        RestReturnResult<SuppliersDetailVO> hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
            hResult = sSuppliersService.getSuppliersById(id);
        return hResult;
    }

    @GetMapping("/getByRelatedId/{relatedId}")
    @ApiOperation(
            value = "供方基本情况对象relatedId获取接口",
            notes = "供方基本情况对对象relatedId获取接口",
            response = SSuppliers.class
    )
    public RestReturnResult<SuppliersDetailVO> getByRelatedId(@PathVariable String relatedId) {
        RestReturnResult<SuppliersDetailVO> hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
            hResult = sSuppliersService.getByRelatedId(relatedId);
        return hResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "供方基本情况添加接口",
            notes = "供方基本情况添加接口",
            response = SSuppliers.class
    )
    public RestReturnResult add(@RequestBody SSuppliers sSuppliers) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersService.insert(sSuppliers);
        restReturnResult.setData(sSuppliers);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "供方基本情况更新接口",
            notes = "供方基本情况更新接口",
            response = SSuppliers.class
    )
    public RestReturnResult update(@RequestBody SSuppliers sSuppliers) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersService.updateById(sSuppliers);
        restReturnResult.setData(sSuppliers);
        return restReturnResult;
    }

    @PostMapping("/public/outerSaveSuppliers")
    @ApiOperation(
            value = "外网供方基本情况添加接口",
            notes = "外网供方基本情况添加接口",
            response = SSuppliers.class
    )
    public RestReturnResult<Object> outerSaveSuppliers(@RequestBody SuppliersDetailVO suppliersDetailVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSuppliersService.outerSaveSuppliers(suppliersDetailVO);
        } catch (RuntimeException e) {
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }

    @PostMapping("/outerUpdateSuppliers")
    @ApiOperation(
            value = "外网供方基本情况更新接口",
            notes = "外网供方基本情况更新接口",
            response = SSuppliers.class
    )
    public RestReturnResult<Object> outerUpdateSuppliers(HttpServletRequest request, HttpServletResponse response, @RequestBody SuppliersDetailVO suppliersDetailVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSuppliersService.outerUpdateSuppliers(request,suppliersDetailVO);
        } catch (RuntimeException e) {
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }

    @PostMapping("/innerUpdateSuppliers")
    @ApiOperation(
            value = "内网供方更新接口",
            notes = "内网供方更新接口",
            response = SSuppliers.class
    )
    public RestReturnResult<Object> innerUpdateSuppliers(@RequestBody SuppliersDetailVO suppliersDetailVO) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSuppliersService.innerUpdateSuppliers(suppliersDetailVO);
        } catch (RuntimeException e) {
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, suppliersDetailVO, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }

//    @PostMapping("/auditSuppliers")
//    @ApiOperation(
//            value = "审核供方信息接口",
//            notes = "审核供方信息接口",
//            response = SSuppliers.class
//    )
//    public RestReturnResult<Object> fallbackSuppliers(@RequestBody QuditRequestVO quditRequestVO) {
//        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, quditRequestVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
//        try {
//            hResult = sSuppliersService.fallbackSuppliers(quditRequestVO.getSupplierId(),quditRequestVO.getAtionType(),"");
//        } catch (RuntimeException e) {
//            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, quditRequestVO, CommonConstants.ERROR_CODES[0], e.getMessage());
//        }
//        return hResult;
//    }

    @GetMapping("/getChangesLog")
    @ApiOperation(
            value = "获取变更记录",
            notes = "获取变更记录",
            response = SSuppliers.class
    )
    public RestReturnResult<Object> getChangesLog(String supplierId,String relatedId) {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, supplierId, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSupplierFormModifyMappingService.getBySupplierId(supplierId,relatedId);
        } catch (Exception e) {
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, supplierId, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "供方基本情况通过ID删除接口",
            notes = "供方基本情况通过ID删除接口",
            response = SSuppliers.class
    )
    public RestReturnResult delete(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sSuppliersService.deleteById(id);
        return restReturnResult;
    }

//    /**
//     * 供方列表-潜在列表(分页)接口
//     *
//     * @param suppliersRequestVO
//     * @return
//     */
//    @ApiOperation(value = "供方列表-潜在列表(分页)接口", notes = "供方列表-潜在列表(分页)接口")
//    @RequestMapping(value = "/getSuppliersMulitList", method = RequestMethod.POST)
//    @ResponseBody
//    public RestReturnResult getSuppliersMulitList(@RequestBody SuppliersRequestVO suppliersRequestVO) {
//        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);
//
//        try {
//            restReturnResult = sSuppliersService.getSuppliersMulitList(suppliersRequestVO);
//        } catch (Exception e) {
//            restReturnResult = new RestReturnResult(CommonConstants.FAIL, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
//            e.printStackTrace();
//        }
//
//        return restReturnResult;
//    }

    /**
     * 供方列表-黑库列表(分页)接口
     *
     * @param suppliersRequestVO
     * @return
     */
    @ApiOperation(value = "供方列表-黑库列表(分页)接口", notes = "供方列表-黑库列表(分页)接口")
    @RequestMapping(value = "/getBlackSuppliersList", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult getBlackSuppliersList(@RequestBody SuppliersRequestVO suppliersRequestVO) {
        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);

        try {
            restReturnResult = sSuppliersService.getSuppliersBlackList(suppliersRequestVO);
        } catch (Exception e) {
            restReturnResult = new RestReturnResult(CommonConstants.FAIL, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
            e.printStackTrace();
        }

        return restReturnResult;
    }

//    /**
//     * 供方列表-其他列表(分页)接口
//     *
//     * @param suppliersRequestVO
//     * @return
//     */
//    @ApiOperation(
//            value = "供方列表-其他列表(分页)接口",
//            notes = "供方列表-其他列表(分页)接口",
//            response = SSuppliers.class
//    )
//    @RequestMapping(value = "/getSuppliersList", method = RequestMethod.POST)
//    @ResponseBody
//    public RestReturnResult getSuppliersList(@RequestBody SuppliersRequestVO suppliersRequestVO) {
//        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);
//
//        try {
//            restReturnResult = sSuppliersService.getSuppliersList(suppliersRequestVO);
//        } catch (Exception e) {
//            e.printStackTrace();
//            restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
//        }
//
//        return restReturnResult;
//    }

    /**
     * 供方列表-其他列表(分页)接口
     *
     * @param suppliersRequestVO
     * @return
     */
    @ApiOperation(
            value = "供方列表-其他列表(分页)接口",
            notes = "供方列表-其他列表(分页)接口",
            response = SSuppliers.class
    )
    @RequestMapping(value = "/getSuppliersList", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult getSuppliersList(@RequestBody SuppliersRequestVO suppliersRequestVO) {
        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);
        try {
            restReturnResult = sSuppliersService.getSuppliersList(suppliersRequestVO);
        } catch (Exception e) {
            e.printStackTrace();
            restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
        }
        return restReturnResult;
    }

    /**
     * 供方列表-其他列表(分页)接口
     *
     * @param suppliersRequestVO
     * @return
     */
    @ApiOperation(
            value = "供方列表-合格",
            notes = "供方列表-合格",
            response = SSuppliers.class
    )
    @RequestMapping(value = "/getQualifiedSuppliersList", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult getQualifiedSuppliersList(@RequestBody SuppliersRequestVO suppliersRequestVO) {
        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);
        try {
            restReturnResult = sSuppliersService.getQualifiedSuppliersList(suppliersRequestVO);
        } catch (Exception e) {
            e.printStackTrace();
            restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
        }
        return restReturnResult;
    }

    @PostMapping("/exportQualifiedSupplier")
    @ApiOperation(value = "合格供应商导出", notes = "合格供应商导出")
    public void exportQualifiedSupplier(@RequestBody SuppliersRequestVO suppliersRequestVO,
                                        HttpServletResponse response, HttpServletRequest request) {
        try {
            String fileName = "合格供应商.xlsx";
            new ExportExcel("合格供应商", SuppliersResponseVO.class).setDataList(sSuppliersService.getQualifiedSuppliersListNoPage(suppliersRequestVO))
                    .write(request,response, fileName).dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(
            value = "供方列表-全部查询接口",
            notes = "供方列表-全部查询接口",
            response = SSuppliers.class
    )
    @RequestMapping(value = "/getSuppliersAll", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult getSuppliersAll(@RequestBody SuppliersRequestVO suppliersRequestVO) {
        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);
        restReturnResult = sSuppliersService.getSuppliersAll(suppliersRequestVO);
        return restReturnResult;
    }


    /**
     * 供方列表-母公司列表(分页)接口
     *
     * @param
     * @return
     */
    @ApiOperation(
            value = "供方列表-母公司列表接口",
            notes = "供方列表-母公司列表接口",
            response = SSuppliers.class
    )
    @RequestMapping(value = "/public/getParentCompanyList", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult getParentCompanyList() {
        RestReturnResult restReturnResult = new RestReturnResult(CommonConstants.SUCCESS, CommonConstants.ERROR_DESCS[0], CommonConstants.ERROR_CODES[0]);

        try {
            restReturnResult = sSuppliersService.getCompanyListByStatus(SupplierStatusEnum.QUALIFIED.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            restReturnResult = new RestReturnResult(CommonConstants.FAIL, CommonConstants.ERROR_DESCS[1], CommonConstants.ERROR_CODES[1]);
        }
        return restReturnResult;
    }


    @GetMapping("/public/checkLoginName/{loginName}/{checkType}")
    @ApiOperation(
            value = "校验登录名",
            notes = "校验登录名"
    )
    public RestReturnResult<SuppliersDetailVO> checkLoginName(@PathVariable("loginName") String loginName, @PathVariable("checkType") int checkType) {
        RestReturnResult<SuppliersDetailVO> hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSuppliersService.checkLoginName(loginName, checkType);
        } catch (Exception e) {
            hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }

    @GetMapping("/public/isExistCompanyName")
    @ApiOperation(
            value = "校验供应商名称是否已经被注册过",
            notes = "校验供应商名称是否已经被注册过"
    )
    public RestReturnResult<Boolean> checkCompanyName(@RequestParam("companyName") String companyName, @RequestParam(value = "id",required = false) String id) {
        RestReturnResult<Boolean> hResult = RestReturnResult.success();
        hResult.setData(sSuppliersService.isExistCompanyName(companyName, id));
        return hResult;
    }

    @PostMapping("/public/checkCodeOrNumber")
    @ApiOperation(
            value = "校验统一社会信用代码/营业执照号",
            notes = "校验统一社会信用代码/营业执照号"
    )
    public RestReturnResult<SuppliersDetailVO> checkCodeOrNumber(@RequestBody SubSupplierRequestVO subSupplierRequestVO) {
        RestReturnResult<SuppliersDetailVO> hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        try {
            hResult = sSuppliersService.checkCodeOrNumber(subSupplierRequestVO);
            
        } catch (Exception e) {
            hResult = new RestReturnResult<SuppliersDetailVO>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], e.getMessage());
        }
        return hResult;
    }
    
    @PostMapping("/getSelectSupplierList")
    @ApiOperation(value = "获取供应商",notes = "获取供应商")
    public RestReturnResult getSelectSupplierList(@RequestBody SelectSupplierListRequestVO vo){
    	RestReturnResult result = RestReturnResult.success();
    	
    	try{
    	    result = sSuppliersService.getSelectSupplierListByCompanyIdAndServiceCategoryIds(vo.getRegionalCompanyId(), vo.getPurchaseClassification());
    	}catch(Exception e){
    		e.printStackTrace();
    		throw new RuntimeException(e.getMessage(), e.getCause());
    	}
    	
    	return result;
    }

}

